package com.sanmubird.mywebframework.eventbus.demo.direct;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.sanmubird.mywebframework.eventbus.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class DemoDirectConsumer extends AbstractMqConsumer<DemoDirectData> {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MqBroker mqBroker;

    @Override
    public MqExchangeEnum getMqRouterEnum() {
        return MqExchangeEnum.DEMO_DIRECT_EXCHANGE;
    }

    @Override
    public List<MqObserver> getMqObserverList() {
        return mqBroker.getMqObserverListBy(getMqRouterEnum().getRoutingKey());
    }

    @Override
    public String fetchMessage() {
        return stringRedisTemplate.opsForList().rightPop(getMqRouterEnum().getQueueName(), TIME_OUT, TimeUnit.SECONDS);
    }

    @Override
    public MqMessage<DemoDirectData> convertToMqMessage(String value) {
        return JSONObject.parseObject(value, new TypeReference<MqMessage<DemoDirectData>>() {
        });
    }
}
